package 题目集.hash;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

/**
 * 思路1：哈希表
 */
//https://pintia.cn/problem-sets/1745607712521764864/exam/problems/1745607748311744522?type=7&page=0
public class 清点代码库 {
    public static void main(String[] args) {
        Reader sc = new Reader();
        int n = sc.nextInt();
        int m = sc.nextInt();
        Map<Node, Integer> map = new HashMap<>();
        for (int i = 0; i < n; i++) {
            int[] params = new int[m];
            for (int j = 0; j < m; j++) {
                params[j] = sc.nextInt();
            }
            Node node = new Node(params);
            map.put(node, map.getOrDefault(node, 0) + 1);
        }
        System.out.println(map.size());
        map.entrySet().stream().sorted((o1, o2) -> {
            Integer v1 = o1.getValue();
            Integer v2 = o2.getValue();
            if (v1.equals(v2)) {
                Node k1 = o1.getKey();
                Node k2 = o2.getKey();
                int i = 0;
                while (k1.params[i]==k2.params[i]){
                    i++;
                }
                return k1.params[i]-k2.params[i];
            }
            return v2-v1;
        }).forEach(o -> System.out.println(o.getValue() + " " + o.getKey()));
//        map.forEach((k, v) -> System.out.println(v + " " + k));
    }

    static class Node {
        int[] params;

        public Node(int[] params) {
            this.params = params;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Node node = (Node) o;
            return Arrays.equals(params, node.params);
        }

        @Override
        public int hashCode() {
            return Arrays.hashCode(params);
        }

        @Override
        public String toString() {
            StringBuilder builder = new StringBuilder();
            for (int param : params) {
                builder.append(param).append(" ");
            }
            builder.deleteCharAt(builder.length() - 1);
            return builder.toString();
        }
    }
    static
    class Reader {
        private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        private StringTokenizer tokenizer = new StringTokenizer("");

        private String innerNextLine() {
            try {
                return reader.readLine();
            } catch (IOException ex) {
                return null;
            }
        }

        public boolean hasNext() {
            while (!tokenizer.hasMoreTokens()) {    //内存中没有
                String nextLine = innerNextLine();
                if (nextLine == null) {     //且没有下一行
                    return false;
                }
                tokenizer = new StringTokenizer(nextLine);  //读入一行
            }
            return true;
        }

        public String nextLine() {
            tokenizer = new StringTokenizer("");    //清空内存中的字符
            return innerNextLine();
        }
        public void clear(){
            tokenizer = new StringTokenizer("");
        }

        public String next() {
            hasNext();
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public double nextDouble(){
            return Double.parseDouble(next());
        }
//    public BigInteger nextBigInt() {
//        return new BigInteger(next());
//    }
    }

}

